<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="产品品牌" prop="prBrand">
        <el-select v-model="queryParams.prBrand" placeholder="请选择">
          <el-option
            v-for="item in listBrand"
            :key="item.id"
            :label="item.brName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品型号" prop="prModel">
        <el-input
          v-model="queryParams.prModel"
          placeholder="请输入产品型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否显示" prop="prShow">
        <el-select
          v-model="queryParams.prShow"
          @change="changeCode"
          placeholder="请选择是否显示"
        >
          <el-option label="显示" value="0"></el-option>
          <el-option label="隐藏" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['applet:model:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="modelList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="产品类型" align="center" prop="prType">
        <template slot-scope="{ row }">
          {{ selectDictLabels(pr_list, String(row.prType)) }}
        </template>
      </el-table-column>

      <el-table-column label="产品品牌" align="center" prop="prBrand">
        <template slot-scope="{ row }">
          <div>{{ getcppp(row.prBrand) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="产品型号" align="center" prop="prModel" />
      <el-table-column label="产品属性" align="center" prop="prProperty" />
      <el-table-column label="产品材质" align="center" prop="prTexture" />
      <el-table-column label="质保年限" align="center" prop="prDeadline" />
      <el-table-column label="价格" align="center" prop="prPrice" />
      <el-table-column label="销量" align="center" prop="prQuantity" />
      <el-table-column label="是否显示" align="center" prop="prShow" />
      <el-table-column label="图标" align="center" prop="prIoc">
        <template slot-scope="{ row }">
          <img :src="row.prIoc" alt="" style="height: 100px" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['applet:model:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['applet:model:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品型号对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div class="flex">
          <div class="flex1">
            <el-form-item label="产品类型" prop="prType">
              <el-select v-model="form.prType" placeholder="请选择">
                <el-option
                  v-for="dict in pr_list"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="产品型号" prop="prModel">
              <el-input v-model="form.prModel" placeholder="请输入产品型号" />
            </el-form-item>
            <el-form-item label="产品材质" prop="prTexture">
              <el-input v-model="form.prTexture" placeholder="请输入产品材质" />
            </el-form-item>
            <el-form-item label="价格" prop="prPrice">
              <el-input v-model="form.prPrice" placeholder="请输入价格" />
            </el-form-item>
            <el-form-item label="是否显示" prop="prShow">
              <el-radio-group v-model="form.prShow">
                <el-radio :label="0">显示</el-radio>
                <el-radio :label="1">隐藏</el-radio>
              </el-radio-group>
            </el-form-item>
          </div>
          <div class="flex1">
            <el-form-item label="产品品牌" prop="prBrand">
              <el-select v-model="form.prBrand" placeholder="请选择">
                <el-option
                  v-for="item in listBrand"
                  :key="item.id"
                  :label="item.brName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="产品属性" prop="prProperty">
              <el-input
                v-model="form.prProperty"
                placeholder="请输入产品属性"
              />
            </el-form-item>
            <el-form-item label="质保年限" prop="prDeadline">
              <el-input
                v-model="form.prDeadline"
                placeholder="请输入质保年限"
              />
            </el-form-item>
            <el-form-item label="销量" prop="prQuantity">
              <el-input v-model="form.prQuantity" placeholder="请输入销量" />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </div>
        </div>
        <div class="flex">
          <div class="flex1">
            <el-form-item label="图标" prop="prIoc">
              <el-upload
                class="avatar-uploader"
                :action="uploadFileUrl"
                :show-file-list="false"
                :headers="headers"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="prIoc" :src="prIoc" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="展示大图" prop="prDetail">
              <el-upload
                class="avatar-uploader"
                :action="uploadFileUrl"
                :show-file-list="false"
                :headers="headers"
                :on-success="handleAvatarSuccess1"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="prDetail" :src="prDetail" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </div>
          <div class="flex1">
            <el-form-item label="产品详情" prop="prParticulars">
              <el-upload
                class="avatar-uploader"
                :action="uploadFileUrl"
                :show-file-list="false"
                :headers="headers"
                :on-success="handleAvatarSuccess2"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="prParticulars" :src="prParticulars" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="施工效果" prop="prResult">
              <el-upload
                class="avatar-uploader"
                :action="uploadFileUrl"
                :show-file-list="false"
                :headers="headers"
                :on-success="handleAvatarSuccess3"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="prResult" :src="prResult" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </div>
        </div>

        <el-form-item label="交车案例" prop="prCase">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileUrl"
            :show-file-list="false"
            :headers="headers"
            :on-success="handleAvatarSuccess4"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="prCase" :src="prCase" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listModel,
  getModel,
  delModel,
  addModel,
  updateModel,
} from "@/api/product/model";
import { listBrand } from "@/api/product/brand";
import { getToken } from "@/utils/auth";

export default {
  name: "model",
  dicts: ["product_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品型号表格数据
      modelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        prType: null,
        prBrand: null,
        prModel: null,
        prProperty: null,
        prTexture: null,
        prDeadline: null,
        prPrice: null,
        prQuantity: null,
        prShow: null,
        prIoc: null,
        prDetail: null,
        prParticulars: null,
        prResult: null,
        prCase: null,
        creationTime: null,
        prStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        prShow: [
          {
            required: true,
            message: "是否显示 0显示 1不显示不能为空",
            trigger: "blur",
          },
        ],
        prStatus: [
          {
            required: true,
            message: "状态 0正常 1删除不能为空",
            trigger: "change",
          },
        ],
      },
      serverUrl: process.env.VUE_APP_BASE_API,
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      prIoc: "", //图标
      prDetail: "", //大图
      prParticulars: "", //产品详情
      prResult: "", //施工效果
      prCase: "", //交车案例
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      listBrand: [], // 产品品牌
      pr_list: [], //产品类型
    };
  },
  async created() {
    //  产品品牌
    let data = await listBrand();
    data.rows.forEach((item) => {
      item.label = item.brName;
      item.value = item.id;
    });
    this.listBrand = data.rows;
    this.getList();
    // 获取字典数据
    this.getDicts("product_type").then((response) => {
      this.pr_list = response.data.map((item) => {
        return {
          label: item.dictLabel,
          value: item.dictCode,
        };
      });
    });
  },

  methods: {
    // 字典匹配数据
    getcppp(id) {
      return this.selectDictLabels(this.listBrand, String(id));
    },

    /** 查询产品型号列表 */
    getList() {
      this.loading = true;
      listModel(this.queryParams).then((response) => {
        response.rows.map((item, index) => {
          item.prShow = item.prShow == 1 ? "隐藏" : "显示";
          item.prIoc = this.serverUrl + item.prIoc; //图标
          item.prDetail = this.serverUrl + item.prDetail; //大图
          item.prParticulars = this.serverUrl + item.prParticulars; //产品详情
          item.prResult = this.serverUrl + item.prResult; //施工效果
          item.prCase = this.serverUrl + item.prCase; //交车案例
        });
        this.modelList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 上传图片
    //图标
    handleAvatarSuccess(res, file) {
      this.prIoc = URL.createObjectURL(file.raw);
      this.form.prIoc = res.fileName;
    },
    //大图
    handleAvatarSuccess1(res, file) {
      this.prDetail = URL.createObjectURL(file.raw);
      this.form.prDetail = res.fileName;
    },
    //产品详情
    handleAvatarSuccess2(res, file) {
      this.prParticulars = URL.createObjectURL(file.raw);
      this.form.prParticulars = res.fileName;
    },
    //施工效果
    handleAvatarSuccess3(res, file) {
      this.prResult = URL.createObjectURL(file.raw);
      this.form.prResult = res.fileName;
    },
    //交车案例
    handleAvatarSuccess4(res, file) {
      this.prCase = URL.createObjectURL(file.raw);
      this.form.prCase = res.fileName;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 200;
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 200MB!");
      }
      return isLt2M;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        prType: null,
        prBrand: null,
        prModel: null,
        prProperty: null,
        prTexture: null,
        prDeadline: null,
        prPrice: null,
        prQuantity: null,
        prShow: 0,
        prIoc: null,
        prDetail: null,
        prParticulars: null,
        prResult: null,
        prCase: null,
        creationTime: null,
        remark: null,
        prStatus: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 选中修改
    changeCode() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品型号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getModel(id).then((response) => {
        this.form = response.data;
        this.prIoc = this.serverUrl + this.form.prIoc; //宣传图
        this.prDetail = this.serverUrl + this.form.prDetail; //大图
        this.prParticulars = this.serverUrl + this.form.prParticulars; //产品详情
        this.prResult = this.serverUrl + this.form.prResult; //施工效果
        this.prCase = this.serverUrl + this.form.prCase; //交车案例
        this.open = true;
        this.title = "修改产品型号";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateModel(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addModel(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
          this.prIoc = ""; //宣传图
          this.prDetail = ""; //大图
          this.prParticulars = ""; //产品详情
          this.prResult = ""; //施工效果
          this.prCase = ""; //交车案例
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除产品型号编号为"' + ids + '"的数据项？')
        .then(function () {
          return delModel(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "applet/model/export",
        {
          ...this.queryParams,
        },
        `model_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
